﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraReports.UserDesigner;
using System.Drawing.Printing;
using BUS;
using DTO;
using System.IO;
using System.Data.SqlClient;
namespace GPBenhLanh
{
    public partial class frmLichSu : DevExpress.XtraEditors.XtraForm
    {
        public frmLichSu()
        {
            InitializeComponent();
            initControl(this);     
        }
        public frmLichSu(string soHS)
        {
            InitializeComponent();
            initControl(this);
            SoHS = soHS;
        }
        private string _SoHS;

        public string SoHS
        {
            get { return _SoHS; }
            set { _SoHS = value; }
        }

        private void initControl(Control ctrl)
        {
            // Bỏ qua điều kiểm trừ khi nó là TextBox.
            if (ctrl.Tag == "text")
            {
                ctrl.Text = "";
            }

            // Xử lý các điều kiểm một cách đệ quy.
            // Điều này cần thiết khi có một điều kiểm chứa nhiều 
            // điều kiểm khác (ví dụ, khi bạn sử dụng Panel, 
            // GroupBox, hoặc điều kiểm container nào khác).
            foreach (Control ctrlChild in ctrl.Controls)
            {
                initControl(ctrlChild);
            }
        }
        public static string _strConnectPK;
        SqlConnection conpk;

        /// <summary>
        /// get thong tin ket noi den database phKham
        /// </summary>
        public void getConnectStringPK()
        {
            string _sConnection = Uit.it_Conn.getConnectString("ConnectPhkham");
            _strConnectPK = axEnCrypt1.DeCrypt(_sConnection, Uit.it_Conn.getPasswordString());
        }

        public void CnPKham()
        {
            if (conpk == null || conpk.State == ConnectionState.Closed)
            {
                getConnectStringPK();
                conpk = new SqlConnection();
                conpk = Uit.it_Conn.getConnect(_strConnectPK);
            }
        }
        public frmLichSu(PrintDocument document)
        {
            InitializeComponent();       
        }
        
        private void frmLichSu_Load(object sender, EventArgs e)
        {
            txtSoHoSo.Text = SoHS;
            CnPKham();
            
            DataTable dtPPNhuom = new DataTable();
            dtPPNhuom = GetThongTinSearchBUS.GetDSPPNhuom();
            comPPNhuom.Properties.DataSource = dtPPNhuom;
            comPPNhuom.Properties.DisplayMember = "TenPPNhuom";
            comPPNhuom.Properties.ValueMember = "IDPPNhuom";
            // Specify the separator character.
            comPPNhuom.Properties.SeparatorChar = ';';
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            if (txtSoHoSo.Text != "")
            {
                string SHS = txtSoHoSo.Text;
                DataTable dt = KQBUS.GetKetQua(2, SHS);
                //gn.NamSinh+'('+gn.Tuoi+')'
                if (dt.Rows.Count > 0)
                {
                    DataRow dr = dt.NewRow();
                    dr = dt.Rows[0];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if(Convert.ToBoolean(dt.Rows[i]["GioiTinh"]))
                            dt.Rows[i]["Gioi"] = "Nam";
                        else
                            dt.Rows[i]["Gioi"] = "Nữ";

                        dt.Rows[i]["nsTuoi"] = dt.Rows[i]["NamSinh"] + "(" + dt.Rows[i]["Tuoi"] + ")";
                        dt.Rows[i]["KQDaiThe"] = Uit.it_Font.VNIToUnicode(dt.Rows[i]["KQDaiThe"]);
                        dt.Rows[i]["KQViThe"] = Uit.it_Font.VNIToUnicode(dt.Rows[i]["KQViThe"]);
                        dt.Rows[i]["KetLuan"] = Uit.it_Font.VNIToUnicode(dt.Rows[i]["KetLuan"]);
                    }

                    if (dr != null)
                    {
                        txtHoTen.Text = dr["HoTen"].ToString();
                        txtTuoi.Text = dr["nsTuoi"].ToString();
                        txtGioi.Text = dr["Gioi"].ToString();
                        txtSoTieuBan.Text = dr["SoTieuBan"].ToString();
                        txtsoXanh.Text = dr["SoXanh"].ToString();
                        txtSoDT.Text = dr["DienThoai"].ToString();
                        txtSoNVien.Text = dr["SoNhapVien"].ToString();
                        txtSoBN.Text = dr["SoBN"].ToString();
                        txtdiachi.Text = dr["DiaChi"].ToString();

                    }
                    gridMain.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("Số hồ sơ:" + SHS + " chưa thực hiện GPB lần nào!", "Thông báo");
                }
            }
            else
            {
                MessageBox.Show("Bạn phải nhập số hồ sơ trước khi tìm!", "Thông báo");
            }
        }

        private void gridMain_Click(object sender, EventArgs e)
        {
            DataRow dr = griddsBenhNhan.GetDataRow(griddsBenhNhan.FocusedRowHandle);
           
        }

        private void txtSoHoSo_Enter(object sender, EventArgs e)
        {
           
        }

        private void txtSoHoSo_Leave(object sender, EventArgs e)
        {
            btnTim_Click(sender, e);
        }

        private void BindataToReport(rptPhieuKQ rptKQ, DataRow dr)
        {
            string url_1 = "";
            string url_2 = "";
            string gioi = "";
            DateTime cdate = DateTime.Today;
            rptKQ.txtSoTieuBan.Text = dr["SoTieuBan"].ToString();
            rptKQ.txtDiaChi.Text = dr["DiaChi"].ToString();
                    
            string dait, kl;
            dait = Uit.it_Font.UnicodeToVNI(dr["KQDaiThe"]);
            if (dait == "NNull")
                dait = "";

            rptKQ.txtdaithe.Text = dait;

            string vt = Uit.it_Font.UnicodeToVNI(dr["KQViThe"]);
            rptKQ.txtVithe.Text = vt;

            if (vt == "NNull")
            {
                rptKQ.txtVithe.Text = "";
                rptKQ.lblViThe.Visible = false;
            }

            string skl = Uit.it_Font.UnicodeToVNI(dr["KetLuan"]);
            if (dait == "NNull")
                skl = "";
            if (skl.Trim().Equals("") || skl.Trim().Equals("NNull"))
            {
                rptKQ.txtketluan.Text = "";
                rptKQ.lblketluan.Visible = false;
            }
            else
                rptKQ.txtketluan.Text = skl;

            rptKQ.txtDViChiDinh.Text = dr["TenDViChiDinh"].ToString();
            rptKQ.txtnghe.Text = dr["DienThoai"].ToString();
            rptKQ.txtchatBP.Text = Uit.it_Font.Vni2Unicode(dr["ChatBP"].ToString(), false);

            if (Convert.ToBoolean(dr["GioiTinh"].ToString()))
                gioi = "Nam";
            else
                gioi = "Nữ";

            rptKQ.barSoHS.Text = dr["SoHS"].ToString();
            rptKQ.txtSoBN.Text = dr["SoBN"].ToString();
            rptKQ.txthoten.Text = dr["HoTen"].ToString();
            rptKQ.txtGioiTinh.Text = gioi;
            rptKQ.txttuoi.Text = dr["NamSinh"].ToString()
                                + "(" + dr["Tuoi"].ToString() + " Tuổi)";
            rptKQ.txtchuandoan.Text = dr["ChuanDoan"].ToString();
            DateTime ngaylm = new DateTime();
            ngaylm = Convert.ToDateTime(dr["NgayLayMau"].ToString());
            rptKQ.txtngaylaymau.Text = ngaylm.ToString("dd/MM/yyyy");
            rptKQ.txtptv.Text = dr["PTVien"].ToString();
            rptKQ.txtppmo.Text = Uit.it_Font.Vni2Unicode(dr["PPMo"], false);

            cdate = DateTime.Parse(dr["CDate"].ToString());
            rptKQ.txttendv.Text = "Đơn vị gửi mẫu:";
            rptKQ.txtketqualan.Text = "KẾT QUẢ";
            rptKQ.txtngaythang.Text = "Ngày " + cdate.Day.ToString("00") +
                                  " Tháng " + cdate.Month.ToString("00") +
                                  " Năm " + cdate.Year.ToString("0000");

            DateTime ngaynhan = new DateTime();
            ngaynhan = Convert.ToDateTime(dr["NgayNhan"].ToString());
            rptKQ.txtngaynhanmau.Text = ngaynhan.ToString("dd/MM/yyyy, hh:mm");

            //PP nhuom
            string idppn = dr["IDPPNhuom"].ToString();
            comPPNhuom.SetEditValue(idppn);
            rptKQ.txtPPNhuom.Text = comPPNhuom.Text;

            if (dr["SlgMau"].ToString() != "")
                rptKQ.txtSoMau.Text = dr["SlgMau"].ToString();
            else
                rptKQ.txtSoMau.Text = "1";
            rptKQ.txtSoLam.Text = dr["SoLam"].ToString();
            rptKQ.txtNguoiCatLoc.Text = dr["HoTenCat"].ToString();
            if (dr["NgayCat"].ToString() != "")
                rptKQ.txtNgayCat.Text = Convert.ToDateTime(dr["NgayCat"]).ToString("dd/MM/yyyy");
            else
                rptKQ.txtNgayCat.Text = "";

            rptKQ.txtNguoiLamTB.Text = dr["HoTenKTV"].ToString();

            if (dr["NgayLenLam"].ToString() != "")
                rptKQ.txtNgayLam.Text = Convert.ToDateTime(dr["NgayLenLam"]).ToString("dd/MM/yyyy");
            else
                rptKQ.txtNgayLam.Text = "";

            string STB = dr["SoTieuBan"].ToString();
            url_1 = hensu.url + ngaynhan.Year + "\\"
                        + ngaynhan.Year.ToString().Substring(2, 2) + ngaynhan.Month.ToString("00")
                        + "\\x100_" + STB + ".jpg";

            url_2 = hensu.url + ngaynhan.Year + "\\"
            + ngaynhan.Year.ToString().Substring(2, 2) + ngaynhan.Month.ToString("00")
            + "\\x400_" + STB + ".jpg";

            if (File.Exists(url_1))
                rptKQ.piclan1.Image = new Bitmap(url_1);
            else
                rptKQ.piclan1.Visible = false;

            if (File.Exists(url_2))
                rptKQ.piclan2.Image = new Bitmap(url_2);
            else
                rptKQ.piclan2.Visible = false;

            if (rptKQ.piclan1.Image == null)
            {
                rptKQ.piclan1.Visible = false;
            }
            if (rptKQ.piclan2.Image == null)
            {
                rptKQ.piclan2.Visible = false;
            }
            rptKQ.txtngaygioin.Text = DateTime.Now.ToString("dd/MM/yyyy, hh:mm:ss");
            rptKQ.txtbsthuchien.Text = dr["HoTenBSTH"].ToString();
            rptKQ.txtThuKy.Text = UserDTO.Name;
            if (dr["SoLamHM"].ToString() != "" && dr["SoLamHM"].ToString() != "0")
                rptKQ.txtGhiChu.Text = dr["SoLamHM"].ToString() + " tiêu bản HMMD";
            else
                rptKQ.txtGhiChu.Text = "";
            if (dr["SlgMauCat"].ToString() != "")
                rptKQ.txtSoMau.Text = dr["SlgMauCat"].ToString();
            else
                rptKQ.txtSoMau.Text = "";

            string SNVien = dr["SoNhapVien"].ToString();
            if (SNVien != "")
            {
                string sql = ""
                                   + "SELECT                   \n"
                                   + "	sonhapvien              \n"
                                   + "	,traibenh               \n"
                                   + "	,dv.TenDonVi            \n"
                                   + "FROM nhapvien            \n"
                                   + "INNER JOIN his.dbo.utb_DM_DonVi AS dv ON traibenh = dv.MaDonVi\n"
                                   + "WHERE sonhapvien = '" + SNVien + "'\n";
                DataTable dttraiBenh = Uit.it_SQL.getDataTable(sql, conpk);
                if (dttraiBenh != null)
                {
                    rptKQ.txtTraiBenh.Text = dttraiBenh.Rows[0]["TenDonVi"].ToString() + " ";
                }
                else
                {
                    rptKQ.txtTraiBenh.Text = "";
                }
            }
        }
        private void btnXem_Click(object sender, EventArgs e)
        {
            DataRow dr = griddsBenhNhan.GetDataRow(griddsBenhNhan.FocusedRowHandle);
            if (dr != null)
            {
               
                rptPhieuKQ rptKQ = new rptPhieuKQ();
                BindataToReport(rptKQ, dr);
                rptKQ.CreateDocument();
                rptKQ.ShowPreview();
            }
        }

        public int SerDataToReport_History_GPB(rptPhieuKQ rptKQ)
        {
            DateTime cdate = DateTime.Today;
            DataTable dt = (DataTable)gridMain.DataSource;
            DataRow dr = dt.NewRow();

            if (dt.Rows.Count > 0)
            {
                dr = dt.Rows[0];
               
                BindataToReport(rptKQ, dr);
                rptKQ.CreateDocument();
                for (int i = 1; i <= dt.Rows.Count - 1; i++)
                {
                    dr = dt.NewRow();
                    dr = dt.Rows[i];
                   
                    rptPhieuKQ rptKQ2 = new rptPhieuKQ();
                    
                    BindataToReport(rptKQ2, dr);
                    rptKQ2.CreateDocument();
                    // Add all pages of the 2nd report to the end of the 1st report.
                    rptKQ.Pages.AddRange(rptKQ2.Pages);

                }
            }
            else
            {
                return 1;
            }

            return 0;
        }
        
        private void btnXemAll_Click(object sender, EventArgs e)
        {
            try
            {
                // Create the 1st report and generate its document.
                rptPhieuKQ report1 = new rptPhieuKQ();
                report1.CreateDocument();
                            
                SerDataToReport_History_GPB(report1);
                // Reset all page numbers in the resulting document.
                report1.PrintingSystem.ContinuousPageNumbering = true;

                // Show the Print Preview form.
                report1.ShowPreviewDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Thông Báo");
            }
        }

        private void txtSoHoSo_MouseDown(object sender, MouseEventArgs e)
        {
            txtSoHoSo.SelectAll();
        }
    }
}